<template>
	<view>
		<div class="auth-msg" v-if='showauth'>
			<div class="auth-msg-m">
				<div class="auth-title">
					权限申请
				</div>
				<div class="auth-msg-c">
					为了保证信息发布的真实性,准确性,我们将收集你的微信名及头像信息。我们收集的个人信息将不会透露给任何第三方(依据法律要求透露的除外)
				</div>
				
				<div class="auth-btn-w">
					<button class="sqbtn" open-type="getUserInfo" @getuserinfo="getuserinfo" >授权</button>
					<button class="sqbtn sqbtn2" @click="goback">拒绝</button>
				</div>
				
			</div>
		</div>
		
		<div class="send-w">
			<div class="send-msg1" style="text-align: center;line-height: 60rpx;padding-top: 60rpx;">为了精准发布 只能选择当前2km内的位置发布</div>
			<div class="send-btn-w">
				<image src="cloud://xiaodule-ouazr.7869-xiaodule-ouazr-1251611099/send.png" mode="widthFix" style="width: 100%;height: 299rpx;"></image>
				<div class="send-btn-m">
					<div class="send-btn-i" @click="gopage('send/send2?id=1')"></div>
					<div class="send-btn-i" @click="gopage('send/send2?id=2')"></div>
					<div class="send-btn-i" @click="gotg"></div>
				</div>
				
			</div>
			
			<div class="hb" style="padding: 30rpx;">
				<image src="cloud://xiaodule-ouazr.7869-xiaodule-ouazr-1251611099/haibao.jpg" mode="widthFix" style="width: 100%;"></image>
			</div>
			
			
		</div>
		
		
	</view>
</template>

<script>
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				showauth:false
			}
		},onLoad() {
			if(!this.$localStorage.getItem('userdata')){
				this.showauth=true
			}
		},
		methods: {
			gotg(){
				uni.showModal({
				    title: '提示',
				    content: '目前小程序内投稿没有开放,如需投稿请发送邮件到my@haotown.cn',
					showCancel:false
				});
			},
			getuserinfo(a){
				console.log(a)
				let userinfo=a.mp.detail.userInfo
				wx.cloud.callFunction({
				  name: 'hserve',
				  data: {
					name:'openid'
				  }
				}).then(res=>{
					
					let openid=res.result.openId
					this.$localStorage.setItem('openid',openid)
					console.log('插入')
					
					let data= {
					    'wxname':userinfo.nickName,
						'avatarUrl':userinfo.avatarUrl,
						'city':userinfo.city,
						'province':userinfo.province,
						'country':userinfo.country,
						'gender':userinfo.gender,
						'openid':openid
					  }
					this.$localStorage.setItem('userdata',JSON.stringify(data))
					
					db.collection('user').where({
					  openid:openid
					}).count().then((res)=>{
						if(res.total==0){
							console.log('注册')
							db.collection('user').add({
							  data: data,
							  success: function(res) {
							    // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
							    console.log('注册成功')
								this.showauth=false
							  }
							})
						}else{
							console.log('已经注册过了,更新数据')
							wx.cloud.callFunction({
							  name: 'hserve',
							  data: {
								name:'upuserdata',
								data:data
							  }
							}).then(res2=>{
								console.log('更新结果',res2)
								this.showauth=false
							})
						}
					})
				})
				
			}
			
		}
	}
</script>

<style lang="scss">
.auth-msg{
	background-color: rgba($color: #000000, $alpha: 0.4);
	position: fixed;
	top: 0;
	left: 0;
	height: 100%;
	z-index: 99;
	width: 100%;
	.auth-msg-m{
		position: absolute;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		width: 500rpx;
		height: 500rpx;
		border-radius: 20rpx;
		background-color: #FFFFFF;
		margin: auto;
		overflow: hidden;
		display: flex;
		flex-direction: column;
		.auth-title{
			text-align: center;
			font-size: 40rpx;
			line-height: 80rpx;
			background-color: $maincolor;
			color: #FFFFFF;
		}
		.auth-msg-c{
			padding: 30rpx;
			flex: 1;
		}
		.auth-btn-w{
			display: flex;
			align-items: center;
			justify-content: center;
			.sqbtn{
				flex: 1;
				color:#FFFFFF;
				background-color:$maincolor;
				width:100%;
				border-radius:0;
				text-align: center;
				&.sqbtn2{
					background-color: #DD524D;
				}
			}
		}
		
		
	}
}
.send-btn-w{
	
	width: 750rpx;
	height: 299rpx;
	background-size: cover;
	position: relative;
	.send-btn-m{
		position: absolute;
		width: 100%;
		height: 100%;
		left: 0;
		bottom: 0;
		z-index: 6;
		display: flex;
		.send-btn-i{
			flex: 1;
			height: 299rpx;
		}
	}
}
</style>
